#include "diameterpiechart.h"



void DiameterPieChart::UpdateSlice(std::vector<std::pair<std::string, int>> Items){




chart->setAnimationOptions(QChart::NoAnimation);
    size_t SerialsSliceNumber = series->slices().size();
    if(Items.size()>AllowMaxSlice) return;
    if(Items.size() != static_cast<size_t>(SerialsSliceNumber)){
        return;
    }
    try{
        long long T_ = 0;
        Total = 0.0;


        for(int i =0;i<SerialsSliceNumber;i++){
            T_ += Items.at(i).second;
        }

        Total = static_cast<double>(T_);//<>
        double Var = 0.0;
        for (size_t i = 0;i<SerialsSliceNumber;i++) {

            series->slices().at(static_cast<int>(i))->setValue(Items.at(i).second);          
            Var = -1.0;
            if(Total < 0.00001){
                   Var = -1.0;
            }
            else {
                Var = Items.at(i).second*100/Total;
                Var =10*(int)Var/10.0;
              // printf_s("var is  %f ratio is %f \n",series->slices().at(i)->value(), Var);
            }


            QString Label_Slice = QString::fromStdString(Items.at(i).first);
            if(Var > 0)
            {

#if 0
        series->slices().at(i)->setLabel(Label_Slice+"数量"+QString("%1").arg(series->slices().at(i)->value())\
                                         +"-"+QString("%1").arg(Var)+"%");
#else
        series->slices().at(i)->setLabel("规格:"+Label_Slice +"  " +QString("%1").arg(Var)+"%");
#endif
            }
         //Total += static_cast<double>(Items.at(i).second);
        }
    }catch(std::out_of_range ex)
    {
        throw  std::move(ex);
    }
}

void DiameterPieChart::refreshItem(std::vector<std::pair<std::string, int>> Items)
{
    series->clear();
    Total = 0.0;
    if(Items.size()>AllowMaxSlice) return;
    int i = 0;
    try{
        for (auto item:Items) {
            Total += item.second;
        }
    for(auto item:Items){
        series->append(QString::fromStdString("规格:"+item.first),item.second);
        Total += static_cast<double>(item.second);
        series->slices().at(i)->setColor(Colors.at(i));
        series->slices().at(i)->setLabelColor(Colors.at(i));
        double Var = -1.0;
        if(Total < 0.00001){
               Var = -1.0;
        }
        else {
            Var = series->slices().at(i)->value()/Total*100;
            Var = double(((int)Var*10))/10.0;

        }

        QString Label_Slice = QString::fromStdString(item.first);
        if(Var > 0)
        {

#if 0
        series->slices().at(i)->setLabel(Label_Slice+"数量"+QString("%1").arg(series->slices().at(i)->value())\
                                         +"-"+QString("%1").arg(Var)+"%");
#else

        series->slices().at(i)->setLabel("规格:"+Label_Slice +"  " +QString("%1").arg(Var)+"%");
#endif
}
        series->slices().at(i)->setLabelFont(QFont("微软雅黑"));
        series->slices().at(i)->setLabelVisible(true);

        i++;
    }
    }catch(std::out_of_range ex)
    {
        throw  std::move(ex);
    }

}


DiameterPieChart::DiameterPieChart()
{
    //绘制饼图
    series = new QPieSeries();
    chart = new QChart();
    if(series == nullptr) exit(0);
    int ColorMax = 255*3;
    int ColorMin = 128*3;
    int Co[3]={0,0,0};
    for(int i = 0 ; i<AllowMaxSlice;i++){
        Co[0] = 128+rand()%128;
        Co[1] = 128+rand()%128;
        Co[2] = 128+rand()%128;
        Colors.push_back(QColor(Co[0],Co[1],Co[2]));
    }
    series->setHoleSize(0.0);//孔大小0.0-1.0
    series->setHorizontalPosition(0.5);//水平位置，默认0.5，0.0-1.0
    series->setLabelsPosition(QPieSlice::LabelOutside);
    series->setLabelsVisible(true);
    //series->setLabelsVisible(false);
    //series->setName("");
    //series->setPieEndAngle(350);
    series->setPieSize(0.7);//饼图尺寸，默认0.7
    series->setPieStartAngle(0);//0度为12点钟方向
    series->setVerticalPosition(0.5);
    series->setVisible(true);

    chart->addSeries(series);
    chart->setTitle(QString::fromStdString("丝径数值饼状图"));
    chart->setAnimationOptions(QChart::SeriesAnimations);
    chart->setTheme(QChart::ChartThemeBlueCerulean);//设置系统主题
    //chart->setAnimationOptions(QChart::AllAnimations);//设置启用或禁用动画
    chart->setAnimationOptions(QChart::AllAnimations);//设置启用或禁用动画
    //chart->setBackgroundBrush(QBrush(QColor(170,170,255)));//设置背景色,主题和背景二选一
    //chart->setDropShadowEnabled(true);//是否背景阴影
    chart->setLocalizeNumbers(true);//数字是否本地化
    //chart->legend()->show();//legend是否显示，show和hide
    //chart->createDefaultAxes();//创建默认轴
    chart->setTitleBrush(QBrush(QColor(255,255,255)));//设置标题Brush
    chart->setTitleFont(QFont("微软雅黑",20));//设置标题字体

    chart->legend()->setAlignment(Qt::AlignLeft);//对齐
    //chart->legend()->detachFromChart();
    //chart->legend()->setBackgroundVisible(true);//设置背景是否可视
    //chart->legend()->setAutoFillBackground(true);//设置背景自动填充
    //chart->legend()->setColor(QColor(255,128,128,128));//设置颜色
    //chart->legend()->setContentsMargins(10,10,10,10);//设置边距left,top,right,bottom
    chart->legend()->setLabelColor(QColor(255,128,255));//设置标签颜色
    chart->legend()->setVisible(true);//设置是否可视
    //chart->legend()->setMaximumHeight(50);
    //chart->legend()->setMaximumWidth(120);
    //chart->legend()->setMaximumSize(10000);
    //chart->legend()->setGeometry(50,50,120,50);//设置几何位置x,y,w,h
    //chart->legend()->setBrush(QBrush(QColor(128,128,128,128)));
    //chart->legend()->setPen(QPen(QColor(192,192, 192,192)));
    chart->legend()->setBorderColor(QColor(255,255,170,185));//设置边框颜色
    QFont font = chart->legend()->font();
    font.setItalic(!font.italic());
    chart->legend()->setFont(font);//设置字体为斜体
    font.setPointSizeF(12);
    chart->legend()->setFont(font);//设置字体大小
    chart->legend()->setFont(QFont("微软雅黑"));//设置字体类型
    chartView = new QChartView(chart);
    chartView->setRenderHint(QPainter::Antialiasing);
}

QChartView *DiameterPieChart::GetQChartView()
{
    return chartView;
}
